import pandas as pd

# Pandas Series对象拼接
pd_series_1 = pd.Series([10, 20, 30, 40], index=list('abcd'))
pd_series_2 = pd.Series([60, 70, 80, 90], index=list('ABCD'))

print(pd.concat([pd_series_1, pd_series_2]))

pd_series_3 = pd.Series([90, 100], index=['I', 'J'])
print(pd.concat([pd_series_1, pd_series_2, pd_series_3]))


# Pandas DataFrame对象拼接， 默认相同的列索引进行合并
def create_dataframe(columns, index):
    """
创建Pandas DataFrame对象
    :param columns: 列数据
    :param index: 行索引
    :return: Pandas DataFrame
    """
    data_dict = {c: [c + str(i) for i in index] for c in columns}
    print(data_dict)
    return pd.DataFrame(data_dict, index=index)


data_frame_1 = create_dataframe('AB', [1, 2])
data_frame_2 = create_dataframe('AB', [11, 22])

# 默认相同的列索引进行合并
print(pd.concat([data_frame_1, data_frame_2]))

data_frame_3 = create_dataframe('CD', [1, 2])
# 相同的行索引进行合并
print(pd.concat([data_frame_1, data_frame_3], axis=1))

# 解决DataFrame拼接重复索引的问题-方案一 ignore_index
print(pd.concat([data_frame_1, data_frame_3], ignore_index=True))
# 解决DataFrame拼接重复索引的问题-方案二 keys
print(pd.concat([data_frame_1, data_frame_3], keys=list('xy')))

print('*' * 50)

# pd.merge合并，与concat拼接类似，与SQL的left\right\inner\outer join类型
# inner: 内连接，只保留相同的
inner_merge = pd.merge(create_dataframe('AB', [1, 2]), create_dataframe('AB', [11, 2]), how='inner')
print(inner_merge)
# outer: 外连接，只保留不同的
outer_merge = pd.merge(create_dataframe('AB', [1, 2]), create_dataframe('AB', [11, 2]), how='outer')
print(outer_merge)
